﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using C1.WPF.Chart.Annotation;

using System.Collections.ObjectModel;
using Model.Modules;
using static ViewModel.VM_PageMain;
using ViewModel;

namespace SteelDetection.UIControl
{
    /// <summary>
    /// UCSteel.xaml 的交互逻辑
    /// </summary>
    public partial class UCSteel : UserControl
    {
        int steelcount = 4;
        int steellength = 100;//暂时按照米


        LinearGradientBrush steelbrush;
        public UCSteel()
        {
            InitializeComponent();

            steelmodel.Rendering += Steelmodel_Rendering;
            steelmodel.Rendered += Steelmodel_Rendered;
            steelmodel.Series.Add(new C1.WPF.Chart.Series() {  Binding="Y"});
            steelmodel.BindingX = "X";


            //steelmodel.ItemsSource = new Collection<Point>() { new Point(0, 4) };

            var df=steelmodel.Layers.Count();
           

            Loaded += UCSteel_Loaded;
            steelbrush= new LinearGradientBrush();
            steelbrush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FF7C7C7C"), 1));
            steelbrush.GradientStops.Add(new GradientStop(Colors.White, 0.36));
            steelbrush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FFC7C5C5"), 0));
            steelbrush.EndPoint = new Point(0.5, 1);
            steelbrush.StartPoint = new Point(0.5, 0);
            steelbrush.Opacity = 0.3;


        }

        private void ImageDefectlist_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            
        }

        private void UCSteel_Loaded(object sender, RoutedEventArgs e)
        {

        }

        public void AddAnno(Point p)
        {

            var folder = System.Environment.CurrentDirectory;

            BitmapImage bit = new BitmapImage();
            bit.BeginInit();
            bit.UriSource = new Uri(folder + @"\Image\Resources\warning.png");
            bit.DecodePixelWidth = 16;
            bit.EndInit();
            bit.Freeze();


            annolayer.Annotations.Add(
                new C1.WPF.Chart.Annotation.Image()
                {


                    Attachment = C1.Chart.Annotation.AnnotationAttachment.DataCoordinate,
                    Position = C1.Chart.Annotation.AnnotationPosition.Center,
                    Location = p,

                    Source = bit,

                }
            ); ;
        }
        public void AddIconFont(Point p,double type)
        {

            annolayer.Annotations.Add(
                new C1.WPF.Chart.Annotation.Rectangle()
                {


                    Attachment = C1.Chart.Annotation.AnnotationAttachment.DataCoordinate,
                    Position = C1.Chart.Annotation.AnnotationPosition.Center,
                    Location = p,
                    Width=20,
                    Height=20,
                    Content = DataConverter.ConvertTypeToIconStr(type),
                    Style = new C1.WPF.Chart.ChartStyle()
                    {
                        Fill= new SolidColorBrush(Colors.Transparent),
                        Stroke= new SolidColorBrush(Colors.Transparent),

                    },
                    ContentStyle =new C1.WPF.Chart.ChartStyle
                    {
                        FontFamily = new FontFamily("iconfont"),
                        FontSize = 16,
                        Fill = new SolidColorBrush(Colors.Red),
                        Stroke = new SolidColorBrush(Color.FromArgb(150, 150, 255, 255)),
                    },
                    
                    TooltipText = DataConverter.ConvertTypeToName(type)
                }
            );
            if (p.X > steelmodel.AxisX.Max)
                steelmodel.AxisX.Max = p.X + 10000;
        }

        public void ClearAnnos() {
            annolayer.Annotations.Clear();
        }

        private void Steelmodel_Rendered(object sender, C1.WPF.Chart.RenderEventArgs e)
        {
            


        }

        private void Steelmodel_Rendering(object sender, C1.WPF.Chart.RenderEventArgs e)
        {
            var steellength = steelmodel.AxisX.Max;
            for (int i = 1; i < 5; i++)
            {
                var point1 = steelmodel.DataToPoint(new Point(0, i));
                var point2 = steelmodel.DataToPoint(new Point(steellength, i - 1));
                double height = (point2.X - point1.X) / 100;
                double x = point1.X, y = point1.Y - height / 2, w = point2.X - point1.X, h = height;
                e.Engine.SetFill(steelbrush);
                e.Engine.SetStroke(new SolidColorBrush(Colors.DarkGray));
                //e.Engine.DrawEllipse(x + w, point1.Y, height / 3, height / 2);
                e.Engine.SetStrokeThickness(0);
                e.Engine.DrawRect(x, y, w, h);
                e.Engine.SetStrokeThickness(1);
                e.Engine.SetStroke(new SolidColorBrush(Colors.DarkGray));
                e.Engine.SetFill(new SolidColorBrush(Colors.LightGray));
                e.Engine.DrawLine(x, y, x + w, y);
                e.Engine.DrawLine(x, y + h, x + w, y + h);
                //e.Engine.DrawEllipse(x, point1.Y, height / 3, height / 2);

            }
        }

        //public image CurrentImg
        //{
        //    get
        //    {
        //        return (image)GetValue(CurrentImgProperty);
        //    }
        //    set
        //    {
        //        SetValue(CurrentImgProperty, value);
        //        C1.WPF.Chart.Annotation.Rectangle rectangle = new C1.WPF.Chart.Annotation.Rectangle() { Attachment = C1.Chart.Annotation.AnnotationAttachment.DataCoordinate, Position = C1.Chart.Annotation.AnnotationPosition.Right };
        //        rectangle.Location = new Point((double)value.left_edge, 0);
        //        rectangle.Width = (double)(value.right_edge-value.left_edge);
        //        rectangle.Height = 0.5;
        //        rectangle.Style.Fill = new SolidColorBrush(Colors.LightSeaGreen);
        //        annolayer.Annotations.Add(rectangle);
        //    }
        //}
        //public static readonly DependencyProperty CurrentImgProperty = DependencyProperty.RegisterAttached("CurrentImg", typeof(image), typeof(UCSteel), new FrameworkPropertyMetadata(null));




    }
}
